package leetcode.数组;

import java.util.List;

public class 搜索插入位置35 {
    //二分
    public static int searchInsert(int[] nums, int target) {
        int L = 0, R = nums.length - 1;
        while (true) {
            if (L >= R)
                return nums[L] < target ? L+1 : L;
            int mid = L + ((R - L) >> 1);
            if (nums[mid] == target)
                return mid;
            else if (nums[mid] > target) {
                R = mid - 1;
            } else
                L = mid + 1;
        }

    }

    public static void main(String[] args) {
        int res = searchInsert(new int[]{1, 3, 5, 6}, 2);
        System.out.println(res);
    }
}
